﻿using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit.Abstractions;

namespace EFCoreDemo.XUnitTest.LoggerHelper
{
    public class XUnitToFECoreLogger : ILogger
    {
        private readonly string categoryName;
        private readonly ITestOutputHelper output;
        public XUnitToFECoreLogger(string categoryName, ITestOutputHelper output)
        {
            this.categoryName = categoryName;
            this.output = output;
        }
        public bool IsEnabled(LogLevel logLevel) => true;
        public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
        {
            if (categoryName == "Microsoft.EntityFrameworkCore.Database.Command"
                    && logLevel == LogLevel.Information)
            {
                var logContent = formatter(state, exception);
                output.WriteLine(logContent);
            }
        }

        public IDisposable BeginScope<TState>(TState state) => null;
    }
}
